package org.jeecg.modules.demo.soFeiyong.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.cwSaleInvoice.entity.CwSaleInvoice;
import org.jeecg.modules.demo.cwSaleInvoice.mapper.CwSaleInvoiceMapper;
import org.jeecg.modules.demo.soFeiyong.entity.SoFeiyong;
import org.jeecg.modules.demo.soFeiyong.mapper.SoFeiyongMapper;
import org.jeecg.modules.demo.soFeiyong.service.ISoFeiyongService;
import org.jeecg.modules.demo.soFyft.entity.SoFyft;
import org.jeecg.modules.demo.soFyft.mapper.SoFyftMapper;
import org.jeecg.modules.demo.soSaleOrder.mapper.SoSaleOrderMapper;
import org.jeecg.modules.demo.util.generateOddUtilXs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: so_feiyong
 * @Author: jeecg-boot
 * @Date: 2022-01-18
 * @Version: V1.0
 */
@Service
public class SoFeiyongServiceImpl extends ServiceImpl<SoFeiyongMapper, SoFeiyong> implements ISoFeiyongService {

    @Resource
    private SoFeiyongMapper soFeiyongMapper;
    @Resource
    private CwSaleInvoiceMapper cwSaleInvoiceMapper;
    @Resource
    private SoFyftMapper soFyftMapper;
    @Resource
    private SoSaleOrderMapper soSaleOrderMapper;

    @Override
    public SoFeiyong getByInvoiceNo(String invoiceNo) {
        return soFeiyongMapper.getByInvoiceNo(invoiceNo);
    }

    @Override
    public String getMaxCode(){
        return soFeiyongMapper.getMaxCode();
    }

    @Override
    @Transactional
    public boolean buildFt(JSONObject jsonObject, HttpServletRequest req) {
        try {
            SoFeiyong soFeiyong = JSON.parseObject(jsonObject.toJSONString(), SoFeiyong.class);
            CwSaleInvoice cs = JSON.parseObject(jsonObject.toJSONString(), CwSaleInvoice.class);

            soFeiyong.setSyFtjine(soFeiyong.getSyFtjine().subtract(cs.getFtJine()));
            if (soFeiyong.getSyFtjine().compareTo(BigDecimal.valueOf(0)) < 1){  //剩余分摊金额<=0
                soFeiyong.setIsFt(2);
            } else {
                if (soFeiyong.getSyFtjine().compareTo(soFeiyong.getTaxamount()) == -1) {  //剩余分摊金额<分摊总金额
                    soFeiyong.setIsFt(1);
                } else {
                    soFeiyong.setIsFt(0);
                }
            }

            CwSaleInvoice cw = cwSaleInvoiceMapper.getByInvoiceNo(soFeiyong.getFpNo());
            if (cw.getFtJine() == null || "".equals(cw.getFtJine())) {
                cw.setFtJine(cs.getFtJine());
            } else {
                cw.setFtJine(cw.getFtJine().add(cs.getFtJine()));
            }
            cwSaleInvoiceMapper.updateById(cw);

            SoFyft sf = new SoFyft();
            String maxCode = soFyftMapper.getMaxCode();
            Map<String,String> codeMap = new HashMap<>();
            if(maxCode == null){
                codeMap.put("maxCode",null);
            }else{
                codeMap.put("maxCode",maxCode);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId);
            String xsft = generateOddUtilXs.createCode(codeMap, tname + "FT_");

            sf.setFtNo(xsft);
            sf.setFyName(soFeiyong.getFyName());
            sf.setFpNo(cw.getInvoiceNo());
            sf.setFtJine(cs.getFtJine());
            sf.setSoFyNo(soFeiyong.getSoFyNo());
            sf.setSofDate(new Date());
            sf.setShenhe(0);
            sf.setIsDelete(0);
            sf.setCinvname(cs.getPrdName());
            sf.setStaff(soFeiyong.getSalesStaff());
            sf.setDept(soFeiyong.getDept());
            soFyftMapper.insert(sf);
            soFeiyong.setFpNo(null);
            soFeiyongMapper.updateById(soFeiyong);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<SoFyft> getFyxxBysoNo(String id) {
        return soFeiyongMapper.getFyxxBysoNo(id);
    }
}
